/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.stock.service;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.thinkgem.jeesite.modules.act.service.ActTaskService;
import com.thinkgem.jeesite.modules.stock.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.stock.dao.MoveStockDao;
import com.thinkgem.jeesite.modules.stock.dao.MoveStockDetailDao;

/**
 * 移库Service
 * @author 移库
 * @version 2019-12-21
 */
@Service
@Transactional(readOnly = true)
public class MoveStockService extends CrudService<MoveStockDao, MoveStock> {

	@Autowired
	private MoveStockDetailDao moveStockDetailDao;
	
	public MoveStock get(String id) {
		MoveStock moveStock = super.get(id);
		moveStock.setMoveStockDetailList(moveStockDetailDao.findList(new MoveStockDetail(moveStock)));
		return moveStock;
	}
	
	public List<MoveStock> findList(MoveStock moveStock) {
		return super.findList(moveStock);
	}
	
	public Page<MoveStock> findPage(Page<MoveStock> page, MoveStock moveStock) {
		return super.findPage(page, moveStock);
	}

	@Autowired
	private ActTaskService actTaskService;
	@Transactional(readOnly = false)
	public void save(MoveStock moveStock) {


			if (StringUtils.isBlank(moveStock.getId())) {
				moveStock.preInsert();
				dao.insert(moveStock);
				// 启动流程
				actTaskService.startProcess("movestock", "s_move_stock", moveStock.getId(), "移库审批");



			}
			else{
				moveStock.preUpdate();
				dao.update(moveStock);
				moveStock.getAct().setComment(("yes".equals(moveStock.getAct().getFlag())?"[重申] ":"[销毁] ")+"盘点审批");

				// 完成流程任务
				Map<String, Object> vars = Maps.newHashMap();
				vars.put("pass", "yes".equals(moveStock.getAct().getFlag())? "1" : "0");
				actTaskService.complete(moveStock.getAct().getTaskId(), moveStock.getAct().getProcInsId(), moveStock.getAct().getComment(), "盘点审批", vars);

			}
		for (MoveStockDetail moveStockDetail : moveStock.getMoveStockDetailList()){
			if (moveStockDetail.getId() == null){
				continue;
			}
			if (MoveStockDetail.DEL_FLAG_NORMAL.equals(moveStockDetail.getDelFlag())){
				if (StringUtils.isBlank(moveStockDetail.getId())){
					moveStockDetail.setMoveid(moveStock);
					moveStockDetail.preInsert();
					moveStockDetailDao.insert(moveStockDetail);
				}else{
					moveStockDetail.preUpdate();
					moveStockDetailDao.update(moveStockDetail);
				}
			}else{
				moveStockDetailDao.delete(moveStockDetail);
			}
		}


	}
	
	@Transactional(readOnly = false)
	public void delete(MoveStock moveStock) {
		super.delete(moveStock);
		moveStockDetailDao.delete(new MoveStockDetail(moveStock));
	}

	@Transactional(readOnly = false)
    public void auditSave(MoveStock pd) {
		// 设置意见
		pd.getAct().setComment(("yes".equals(pd.getAct().getFlag())?"[同意] ":"[驳回] ")+pd.getAct().getComment());

		// 对不同环节的业务逻辑进行操作
		String taskDefKey = pd.getAct().getTaskDefKey();
		// 审核环节
		if ("audit1".equals(taskDefKey)){


		}
		else if ("audit2".equals(taskDefKey)){
			if("yes".equals(pd.getAct().getFlag())) {
				for (MoveStockDetail moveStockDetail : pd.getMoveStockDetailList()) {

					moveStockDetailDao.updateCon(moveStockDetail);

				}
			}

		}
		else if ("audit3".equals(taskDefKey)){

			if("yes".equals(pd.getAct().getFlag())){
				for (MoveStockDetail moveStockDetail : pd.getMoveStockDetailList()) {

					moveStockDetailDao.updateTnmae(moveStockDetail);

				}
				/*for (SpdDetail spdDetail : pd.getSpdDetailList()){
					Stock s = new Stock();
					s.setW(spdDetail.getWoods());
					List<Stock> ss=stockService.findList(s);
					if(ss!=null&&ss.size()>0){
						Stock stock2=ss.get(0);
						stock2.setNumber(stock2.getNumber()+spdDetail.getFactnumber()-spdDetail.getFactnumber());
						stockService.save(stock2);
					}
					StockWarehouse stockWarehouse=new StockWarehouse();
					stockWarehouse.setWid(spdDetail.getWoods());
					stockWarehouse.setWhid(pd.getLoc());
					List<StockWarehouse> sws=stockWarehouseService.findList(stockWarehouse);
					if(sws!=null&&sws.size()>0){
						stockWarehouse=sws.get(0);
						stockWarehouse.setNumber(stockWarehouse.getNumber()+spdDetail.getFactnumber()-spdDetail.getFactnumber());
						stockWarehouseService.save(stockWarehouse);
					}
				}*/
			}



		}
		else if ("audit4".equals(taskDefKey)){


		}
		else if ("audit5".equals(taskDefKey)){
			if("yes".equals(pd.getAct().getFlag())) {
				for (MoveStockDetail moveStockDetail : pd.getMoveStockDetailList()) {
					//老的
					ContainerDetail containerDetail= containerDetailService.get(moveStockDetail.getW().getId());
					containerDetail.setWnumber(containerDetail.getWnumber()-moveStockDetail.getTname());
					containerDetail.setNumber(containerDetail.getNumber()-moveStockDetail.getName());
					containerDetailService.save(containerDetail);

					Container container2=containerService.get(containerDetail.getC().getId());
					container2.setNumber(container2.getNumber()+containerDetail.getNumber());
					containerService.save(container2);

					//新的
					Container container=containerService.get(moveStockDetail.getT().getId());
					ContainerDetail containerDetail2=new ContainerDetail();
					containerDetail2.setWoodsid(containerDetail.getWoodsid());
					containerDetail2.setC(container);
					containerDetail2.setName(containerDetail.getName());
					containerDetail2.setW(containerDetail.getW());
					containerDetail2.setNumber(moveStockDetail.getName());
					containerDetail2.setWnumber(moveStockDetail.getTname());
					container.setNumber(container.getNumber()-containerDetail.getNumber());
					containerService.save(container);
					containerDetailService.save(containerDetail2);
				}
			}

		}


		// 提交流程任务
		Map<String, Object> vars = Maps.newHashMap();
		vars.put("pass", "yes".equals(pd.getAct().getFlag())? "1" : "0");
		actTaskService.complete(pd.getAct().getTaskId(), pd.getAct().getProcInsId(), pd.getAct().getComment(), vars);
    }

	@Autowired
	private ContainerService containerService;
	@Autowired
	private ContainerDetailService containerDetailService;
}